package net.zyj.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import net.sf.json.JSONArray;
import net.zyj.student.mapper.ClassMapper;
import net.zyj.student.pojo.Teacher;

public class ClassAction extends HttpServlet {
	
	ClassMapper classMapper = new ClassMapper();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getParameter("action").equalsIgnoreCase("selectByTeacherid")){
			selectByTeacherid(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("add")){
			
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	/**
	 * 根据教师id查找所管理的班级
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void selectByTeacherid(HttpServletRequest request, HttpServletResponse response) throws IOException {
			
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			
			Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
			List<Teacher> list = classMapper.selectByTeacherid(teacher.getId());
			JSONArray json = JSONArray.fromObject(list);
			out.print(json);
			
			out.flush();
			out.close();
		}
}
